package vn.cuhanh;

import java.util.ArrayList;
import java.util.List;

import vn.cuhanh.utils.CuhanhDao;
import vn.cuhanh.utils.HelperFunc;
import vn.cuhanh.utils.ZGExceptionHandler;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.analytics.tracking.android.EasyTracker;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class BaseActivity extends SlidingFragmentActivity {

	private int mTitleRes;
	protected ListFragment mFrag;
	public static boolean isOnline = true;
	public static String sourceInstall;
	public static String mDeviceID;
	public static String deviceImei;
	public static final int NOTI_UPDATE_ID = 1;
	public static List<String> logData = new ArrayList<String>();

	public BaseActivity(int titleRes) {
		mTitleRes = titleRes;
	}

	public BaseActivity() {

	}

	public int getVersion() {
		int version = -1;
		try {
			PackageInfo pInfo = getPackageManager().getPackageInfo(
					getPackageName(), PackageManager.GET_META_DATA);
			version = pInfo.versionCode;
		} catch (NameNotFoundException e1) {
			HelperFunc.readStackTrace(e1);
		}
		return version;
	}

	@Override
	protected void onStart() {
		super.onStart();
		this.initGoogleAnalytic();
	}
	
	public static void updateTotalScore(Context ctx, int score, View view) {
		if(MainActivity.userAccount != null) {
			MainActivity.userAccount.setDndScore(score);
			CuhanhDao.updateDndScore(ctx, MainActivity.userAccount.getUserName(), score);
		} else {
			HelperFunc.readStackTrace(new NullPointerException("Null pointer: MainActivity.userAccount."));
		}
		if(view != null) {
			BaseActivity.updateDndScore(view);
		}
	}
	
	public static void updateDndScore(View v) {
		try {
			TextView txtDndScore = (TextView)v.findViewById(R.id.txtDndScore);
			if(txtDndScore == null) {
				return;
			}
			txtDndScore.setText(String.valueOf(MainActivity.userAccount != null ? MainActivity.userAccount.getDndScore() : 0));
		} catch (Exception e) {
			HelperFunc.readStackTrace(e);
		}
	}
	
	public void initGoogleAnalytic() {
		if (MainActivity.useGoogleAnalytic) {
			try {
				EasyTracker.getInstance(this).activityStart(this); // Add this
			} catch (Exception e) {
				HelperFunc.readStackTrace(e);
			}
		}
	}
	
	public void stopGoogleAnalytic() {
		if (MainActivity.useGoogleAnalytic) {
			try {
				EasyTracker.getInstance(this).activityStop(this); // Add this
			} catch (Exception e) {
				HelperFunc.readStackTrace(e);
			}
		}
	}

	@Override
	public void onStop() {
		super.onStop();
		this.stopGoogleAnalytic();
	}

	@Override
	public void onResume() {
		/*
		 * if (MainActivity.isClose) { finish(); }
		 */
		super.onResume();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/*
		 * StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
		 * .penaltyLog().penaltyDeath().build());
		 */

		Thread.setDefaultUncaughtExceptionHandler(new ZGExceptionHandler());
		if (mTitleRes != 0) {
			setTitle(mTitleRes);
		}
		// get status network
//		isOnline = getOnlineStatus();

		// get imei device
//		TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//		deviceImei = telephonyManager.getDeviceId();
//		if (deviceImei == null) {
//			deviceImei = Settings.Secure.getString(getContentResolver(),
//					Settings.Secure.ANDROID_ID);
//		}

		// customize the SlidingMenu
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setBehindWidthRes(R.dimen.slidingmenu_width);

		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);

		// actionBar.setIcon(null);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(false);
		View cView = getLayoutInflater().inflate(R.layout.title_bar, null);
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setCustomView(cView);
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setDisplayShowHomeEnabled(false);

		// set event for top bar
		ImageView btn_menu;
		btn_menu = (ImageView) cView.findViewById(R.id.home);
		btn_menu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				toggle();
			}
		});
		
		//init back btn
		ImageView btn_back;
		btn_back = (ImageView) cView.findViewById(R.id.back);
		btn_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				onBackPressed();
			}
		});
		if(isShowBackBtn()) {
			btn_back.setVisibility(View.VISIBLE);
			View divider = cView.findViewById(R.id.title_bar_divider2);
			divider.setVisibility(View.VISIBLE);
		}
	}
	
	public boolean isShowBackBtn() {
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		/*
		 * switch (item.getItemId()) { case android.R.id.home: toggle(); return
		 * true; case R.id.search: Intent i = new Intent(BaseActivity.this,
		 * SearchActivity.class); startActivity(i);
		 * 
		 * return true; default: break; }
		 */
		
//		if (item.getItemId() == android.R.id.home) {
//			toggle();
//			return true;
//		} else {
//			if (item.getItemId() == R.id.search) {
//				Intent i = new Intent(BaseActivity.this, SearchActivity.class);
//				startActivity(i);
//
//				return true;
//			}
//		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// getSupportMenuInflater().inflate(R.menu.main, menu);
//		boolean isLight = true;

		return true;
	}

	private boolean getOnlineStatus() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {

			return true;
		}
		return false;
	}
	
//	MenuFragment mTargetFragment;
//
//	@Override
//	public void onAttachFragment(Fragment fragment) {
//		super.onAttachFragment(fragment);
//
//		String fragmentSimpleName = fragment.getClass().getSimpleName();
//
//		if (fragmentSimpleName.equals("MenuFragment"))
//			mTargetFragment = (MenuFragment) fragment;
//	}
//
//	@Override
//	public void onActivityResult(int requestCode, int resultCode, Intent data) {
//		super.onActivityResult(requestCode, resultCode, data);
//
//		if (mTargetFragment != null)
//			mTargetFragment.myCallBack(requestCode, resultCode, data);
//	}

}
